﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HeritageAcademy.Library.Controls
{
    internal class FlowDocumentPageViewerLight : FlowDocumentPageViewer
    {
		#region Parameters 
        bool _isSelectionHandlerSet;       
        public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FlowDocumentPageViewerLight));
        public static readonly RoutedEvent ZoomChangedEvent = EventManager.RegisterRoutedEvent("ZoomChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FlowDocumentPageViewerLight));
		#endregion

		#region Properties 
        protected override bool HandlesScrolling
        {
            get { return false; }
        }
		#endregion Properties 

		#region Events 
        public event RoutedEventHandler SelectionChanged
        {
            add { AddHandler(SelectionChangedEvent, value); }
            remove { RemoveHandler(SelectionChangedEvent, value); }
        }

        public event RoutedEventHandler ZoomChanged
        {
            add { AddHandler(ZoomChangedEvent, value); }
            remove { RemoveHandler(ZoomChangedEvent, value); }
        }
		#endregion Events 

		#region Event Handlers 
        void Selection_Changed(object sender, EventArgs e)
        {
            RaiseSelectionChangedEvent();
        }
		#endregion Event Handlers 

		#region Protected Methods 
        protected override void OnDocumentChanged()
        {
            base.OnDocumentChanged();
            if (Selection != null)
                Selection.Changed -= Selection_Changed;
            _isSelectionHandlerSet = false;
        }

        protected override void OnFirstPageCommand()
        {
            //base.OnFirstPageCommand();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            //base.OnKeyDown(e);
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            //base.OnPreviewKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            //base.OnKeyUp(e);
        }

        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            //base.OnPreviewKeyUp(e);
        }

        protected override void OnLastPageCommand()
        {
            //base.OnLastPageCommand();
        }

        protected override void OnNextPageCommand()
        {
            //base.OnNextPageCommand();
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (Selection == null || _isSelectionHandlerSet) return;
            Selection.Changed += Selection_Changed;
            _isSelectionHandlerSet = true;
        }

        protected override void OnPreviousPageCommand()
        {
            //base.OnPreviousPageCommand();
        }
		#endregion Protected Methods 

		#region Private Methods 
        void RaiseSelectionChangedEvent()
        {
            var newEventArgs = new RoutedEventArgs(SelectionChangedEvent);
            RaiseEvent(newEventArgs);
        }

        void RaiseZoomChangedEvent()
        {
            var newEventArgs = new RoutedEventArgs(ZoomChangedEvent);
            RaiseEvent(newEventArgs);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == ZoomProperty)
                RaiseZoomChangedEvent();
            base.OnPropertyChanged(e);
        }
		#endregion Private Methods 
    }
}
